/* This is the 'improved version' for mycat.c
 * just substitute the fopen with open, and getc/putc with read/write
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>

void filecopy(int ifd, int ofd);

int main(int argc, char *argv[])
{
  int fd;

  // accept argv[1+] as filenames, and transform them into file descriptors
  for (int i = 1; i < argc; i++) {
    fd = open(argv[i], O_RDONLY);
    filecopy(fd, 1);

    close(fd);
  }

  return EXIT_SUCCESS;
}

void filecopy(int ifd, int ofd) {
  int n;
  char buf[BUFSIZ];
  while ((n = read(ifd, buf, BUFSIZ)) > 0) {
    write(ofd, buf, n);
  }
}
